/**
 * @fileOverview The floating dialog plugin.
 */

VED.DIALOG_RESIZE_NONE = 0;
VED.DIALOG_RESIZE_WIDTH = 1;
VED.DIALOG_RESIZE_HEIGHT = 2;
VED.DIALOG_RESIZE_BOTH = 3;

(function() {
   var cssLength = VED.tools.cssLength;

   function isTabVisible( tabId ) {
      return !!this._.tabs[ tabId ][ 0 ].$.offsetHeight;
   }

   function getPreviousVisibleTab() {
      var tabId = this._.currentTabId,
         length = this._.tabIdList.length,
         tabIndex = VED.tools.indexOf( this._.tabIdList, tabId ) + length;

      for ( var i = tabIndex - 1; i > tabIndex - length; i-- ) {
         if ( isTabVisible.call( this, this._.tabIdList[ i % length ] ) )
            return this._.tabIdList[ i % length ];
      }

      return null;
   }

   function getNextVisibleTab() {
      var tabId = this._.currentTabId,
         length = this._.tabIdList.length,
         tabIndex = VED.tools.indexOf( this._.tabIdList, tabId );

      for ( var i = tabIndex + 1; i < tabIndex + length; i++ ) {
         if ( isTabVisible.call( this, this._.tabIdList[ i % length ] ) )
            return this._.tabIdList[ i % length ];
      }

      return null;
   }


   function clearOrRecoverTextInputValue( container, isRecover ) {
      var inputs = container.$.getElementsByTagName( 'input' );
      for ( var i = 0, length = inputs.length; i < length; i++ ) {
         var item = new VED.dom.element( inputs[ i ] );

         if ( item.getAttribute( 'type' ).toLowerCase() == 'text' ) {
            if ( isRecover ) {
               item.setAttribute( 'value', item.getCustomData( 'fake_value' ) || '' );
               item.removeCustomData( 'fake_value' );
            } else {
               item.setCustomData( 'fake_value', item.getAttribute( 'value' ) );
               item.setAttribute( 'value', '' );
            }
         }
      }
   }

   // Handle dialog element validation state UI changes.
   function handleFieldValidated( isValid, msg ) {
      var input = this.getInputElement();
      if ( input ) {
         isValid ? input.removeAttribute( 'aria-invalid' ) : input.setAttribute( 'aria-invalid', true );
      }

      if ( !isValid ) {
         if ( this.select )
            this.select();
         else
            this.focus();
      }

      msg && alert( msg );

      this.fire( 'validated', { valid: isValid, msg: msg } );
   }

   function resetField() {
      var input = this.getInputElement();
      input && input.removeAttribute( 'aria-invalid' );
   }

   var templateSource = '<div class="ved ved_reset_all {editorId} {editorDialogClass}' +
      '" dir="{langDir}"' +
      ' lang="{langCode}"' +
      ' role="application"' +
      '>' +
      '<table class="ved_dialog ' + VED.env.cssClass + ' ved_{langDir}"' +
         ' aria-labelledby="ved_dialog_title_{id}"' +
         ' style="position:absolute" role="dialog">' +
         '<tr><td role="presentation">' +
         '<div class="ved_dialog_body" role="presentation">' +
            '<div id="ved_dialog_title_{id}" class="ved_dialog_title" role="presentation"></div>' +
            '<a id="ved_dialog_close_button_{id}" class="ved_dialog_close_button" href="javascript:void(0)" title="{closeTitle}" role="button"><span class="ved_label">X</span></a>' +
            '<div id="ved_dialog_tabs_{id}" class="ved_dialog_tabs" role="tablist"></div>' +
            '<table class="ved_dialog_contents" role="presentation">' +
            '<tr>' +
               '<td id="ved_dialog_contents_{id}" class="ved_dialog_contents_body" role="presentation"></td>' +
            '</tr>' +
            '<tr>' +
               '<td id="ved_dialog_footer_{id}" class="ved_dialog_footer" role="presentation"></td>' +
            '</tr>' +
            '</table>' +
         '</div>' +
         '</td></tr>' +
      '</table>' +
      '</div>';

   function buildDialog( editor ) {
      var element = VED.dom.element.createFromHtml( VED.addTemplate( 'dialog', templateSource ).output({
         id: VED.tools.getNextNumber(),
         editorId: editor.id,
         langDir: editor.lang.dir,
         langCode: editor.langCode,
         editorDialogClass: 'ved_editor_' + editor.name.replace( /\./g, '\\.' ) + '_dialog',
         closeTitle: editor.lang.common.close
      }));

      // TODO: Change this to getById(), so it'll support custom templates.
      var body = element.getChild( [ 0, 0, 0, 0, 0 ] ),
         title = body.getChild( 0 ),
         close = body.getChild( 1 );

      // IFrame shim for dialog that masks activeX in IE. (#7619)
      if ( VED.env.ie && !VED.env.ie6Compat ) {
         var isCustomDomain = VED.env.isCustomDomain(),
            src = 'javascript:void(function(){' + encodeURIComponent( 'document.open();' + ( isCustomDomain ? ( 'document.domain="' + document.domain + '";' ) : '' ) + 'document.close();' ) + '}())',
            iframe = VED.dom.element.createFromHtml( '<iframe' +
               ' frameBorder="0"' +
               ' class="ved_iframe_shim"' +
               ' src="' + src + '"' +
               ' tabIndex="-1"' +
               '></iframe>' );
         iframe.appendTo( body.getParent() );
      }

      // Make the Title and Close Button unselectable.
      title.unselectable();
      close.unselectable();

      return {
         element: element,
         parts: {
            dialog: element.getChild( 0 ),
            title: title,
            close: close,
            tabs: body.getChild( 2 ),
            contents: body.getChild( [ 3, 0, 0, 0 ] ),
            footer: body.getChild( [ 3, 0, 1, 0 ] )
         }
      };
   }

   // Focusable interface. Use it via dialog.addFocusable.
   function Focusable( dialog, element, index ) {
      this.element = element;
      this.focusIndex = index;
      // TODO: support tabIndex for focusables.
      this.tabIndex = 0;
      this.isFocusable = function() {
         return !element.getAttribute( 'disabled' ) && element.isVisible();
      };
      this.focus = function() {
         dialog._.currentFocusIndex = this.focusIndex;
         this.element.focus();
      };
      // Bind events
      element.on( 'keydown', function( e ) {
         if ( e.data.getKeystroke() in { 32:1,13:1 } )
            this.fire( 'click' );
      });
      element.on( 'focus', function() {
         this.fire( 'mouseover' );
      });
      element.on( 'blur', function() {
         this.fire( 'mouseout' );
      });
   }

   // Re-layout the dialog on window resize.
   function resizeWithWindow( dialog ) {
      var win = VED.document.getWindow();
      function resizeHandler() { dialog.layout(); }
      win.on( 'resize', resizeHandler );
      dialog.on( 'hide', function() { win.removeListener( 'resize', resizeHandler ); } );
   }

   VED.dialog = VED.event.extend({
      initialize: function( editor, dialogName ) {
         // Load the dialog definition.
         var definition = VED.dialog._.dialogDefinitions[ dialogName ],
            defaultDefinition = VED.tools.clone( defaultDialogDefinition ),
            buttonsOrder = editor.config.dialog_buttonsOrder || 'OS',
            dir = editor.lang.dir,
            tabsToRemove = {},
            i, processed, stopPropagation;

         if( ( buttonsOrder == 'OS' && VED.env.mac ) || // The buttons in MacOS Apps are in reverse order (#4750)
         ( buttonsOrder == 'rtl' && dir == 'ltr' ) || ( buttonsOrder == 'ltr' && dir == 'rtl' ) )
            defaultDefinition.buttons.reverse();


         // Completes the definition with the default values.
         definition = VED.tools.extend( definition( editor ), defaultDefinition );

         // Clone a functionally independent copy for this dialog.
         definition = VED.tools.clone( definition );

         // Create a complex definition object, extending it with the API
         // functions.
         definition = new definitionObject( this, definition );

         var doc = VED.document;

         var themeBuilt = buildDialog( editor );

         // Initialize some basic parameters.
         this._ = {
            editor: editor,
            element: themeBuilt.element,
            name: dialogName,
            contentSize: { width: 0, height: 0 },
            size: { width: 0, height: 0 },
            contents: {},
            buttons: {},
            accessKeyMap: {},

            // Initialize the tab and page map.
            tabs: {},
            tabIdList: [],
            currentTabId: null,
            currentTabIndex: null,
            pageCount: 0,
            lastTab: null,
            tabBarMode: false,

            // Initialize the tab order array for input widgets.
            focusList: [],
            currentFocusIndex: 0,
            hasFocus: false
         };

         this.parts = themeBuilt.parts;

         VED.tools.setTimeout( function() {
            editor.fire( 'ariaWidget', this.parts.contents );
         }, 0, this );

         // Set the startup styles for the dialog, avoiding it enlarging the
         // page size on the dialog creation.
         var startStyles = {
            position: VED.env.ie6Compat ? 'absolute' : 'fixed',
            top: 0,
            visibility: 'hidden'
         };

         startStyles[ dir == 'rtl' ? 'right' : 'left' ] = 0;
         this.parts.dialog.setStyles( startStyles );

         // Call the VED.event constructor to initialize this instance.
         this.parent.call( this );

         // Fire the "dialogDefinition" event, making it possible to customize
         // the dialog definition.
         this.definition = definition = VED.fire( 'dialogDefinition', {
            name: dialogName,
            definition: definition
         }, editor ).definition;

         // Cache tabs that should be removed.
         if ( !( 'removeDialogTabs' in editor._ ) && editor.config.removeDialogTabs ) {
            var removeContents = editor.config.removeDialogTabs.split( ';' );

            for ( i = 0; i < removeContents.length; i++ ) {
               var parts = removeContents[ i ].split( ':' );
               if ( parts.length == 2 ) {
                  var removeDialogName = parts[ 0 ];
                  if ( !tabsToRemove[ removeDialogName ] )
                     tabsToRemove[ removeDialogName ] = [];
                  tabsToRemove[ removeDialogName ].push( parts[ 1 ] );
               }
            }
            editor._.removeDialogTabs = tabsToRemove;
         }

         // Remove tabs of this dialog.
         if ( editor._.removeDialogTabs && ( tabsToRemove = editor._.removeDialogTabs[ dialogName ] ) ) {
            for ( i = 0; i < tabsToRemove.length; i++ )
               definition.removeContents( tabsToRemove[ i ] );
         }

         // Initialize load, show, hide, ok and cancel events.
         if ( definition.onLoad )
            this.on( 'load', definition.onLoad );

         if ( definition.onShow )
            this.on( 'show', definition.onShow );

         if ( definition.onHide )
            this.on( 'hide', definition.onHide );

         if ( definition.onOk ) {
            this.on( 'ok', function( evt ) {
               // Dialog confirm might probably introduce content changes (#5415).
               editor.fire( 'saveSnapshot' );
               setTimeout( function() {
                  editor.fire( 'saveSnapshot' );
               }, 0 );
               if ( definition.onOk.call( this, evt ) === false )
                  evt.data.hide = false;
            });
         }

         if ( definition.onCancel ) {
            this.on( 'cancel', function( evt ) {
               if ( definition.onCancel.call( this, evt ) === false )
                  evt.data.hide = false;
            });
         }

         var me = this;

         // Iterates over all items inside all content in the dialog, calling a
         // function for each of them.
         var iterContents = function( func ) {
               var contents = me._.contents,
                  stop = false;

               for ( var i in contents ) {
                  for ( var j in contents[ i ] ) {
                     stop = func.call( this, contents[ i ][ j ] );
                     if ( stop )
                        return;
                  }
               }
            };

         this.on( 'ok', function( evt ) {
            iterContents( function( item ) {
               if ( item.validate ) {
                  var retval = item.validate( this ),
                     invalid = typeof( retval ) == 'string' || retval === false;

                  if ( invalid ) {
                     evt.data.hide = false;
                     evt.stop();
                  }

                  handleFieldValidated.call( item, !invalid, typeof retval == 'string' ? retval : undefined );
                  return invalid;
               }
            });
         }, this, null, 0 );

         this.on( 'cancel', function( evt ) {
            iterContents( function( item ) {
               if ( item.isChanged() ) {
                  if ( !confirm( editor.lang.common.confirmCancel ) )
                     evt.data.hide = false;
                  return true;
               }
            });
         }, this, null, 0 );

         this.parts.close.on( 'click', function( evt ) {
            if ( this.fire( 'cancel', { hide: true } ).hide !== false )
               this.hide();
            evt.data.preventDefault();
         }, this );

         // Sort focus list according to tab order definitions.
         function setupFocus() {
            var focusList = me._.focusList;
            focusList.sort( function( a, b ) {
               // Mimics browser tab order logics;
               if ( a.tabIndex != b.tabIndex )
                  return b.tabIndex - a.tabIndex;
               //  Sort is not stable in some browsers,
               // fall-back the comparator to 'focusIndex';
               else
                  return a.focusIndex - b.focusIndex;
            });

            var size = focusList.length;
            for ( var i = 0; i < size; i++ )
               focusList[ i ].focusIndex = i;
         }

         function changeFocus( offset ) {
            var focusList = me._.focusList;
            offset = offset || 0;

            if ( focusList.length < 1 )
               return;

            var current = me._.currentFocusIndex;

            // Trigger the 'blur' event of  any input element before anything,
            // since certain UI updates may depend on it.
            try {
               focusList[ current ].getInputElement().$.blur();
            } catch ( e ) {}

            var startIndex = ( current + offset + focusList.length ) % focusList.length,
               currentIndex = startIndex;
            while ( offset && !focusList[ currentIndex ].isFocusable() ) {
               currentIndex = ( currentIndex + offset + focusList.length ) % focusList.length;
               if ( currentIndex == startIndex )
                  break;
            }

            focusList[ currentIndex ].focus();

            // Select whole field content.
            if ( focusList[ currentIndex ].type == 'text' )
               focusList[ currentIndex ].select();
         }

         this.changeFocus = changeFocus;


         function keydownHandler( evt ) {
            // If I'm not the top dialog, ignore.
            if ( me != VED.dialog._.currentTop )
               return;

            var keystroke = evt.data.getKeystroke(),
               rtl = editor.lang.dir == 'rtl',
               button;

            processed = stopPropagation = 0;

            if ( keystroke == 9 || keystroke == VED.SHIFT + 9 ) {
               var shiftPressed = ( keystroke == VED.SHIFT + 9 );

               // Handling Tab and Shift-Tab.
               if ( me._.tabBarMode ) {
                  // Change tabs.
                  var nextId = shiftPressed ? getPreviousVisibleTab.call( me ) : getNextVisibleTab.call( me );
                  me.selectPage( nextId );
                  me._.tabs[ nextId ][ 0 ].focus();
               } else {
                  // Change the focus of inputs.
                  changeFocus( shiftPressed ? -1 : 1 );
               }

               processed = 1;
            } else if ( keystroke == VED.ALT + 121 && !me._.tabBarMode && me.getPageCount() > 1 ) {
               // Alt-F10 puts focus into the current tab item in the tab bar.
               me._.tabBarMode = true;
               me._.tabs[ me._.currentTabId ][ 0 ].focus();
               processed = 1;
            } else if ( ( keystroke == 37 || keystroke == 39 ) && me._.tabBarMode ) {
               // Arrow keys - used for changing tabs.
               nextId = ( keystroke == ( rtl ? 39 : 37 ) ? getPreviousVisibleTab.call( me ) : getNextVisibleTab.call( me ) );
               me.selectPage( nextId );
               me._.tabs[ nextId ][ 0 ].focus();
               processed = 1;
            } else if ( ( keystroke == 13 || keystroke == 32 ) && me._.tabBarMode ) {
               this.selectPage( this._.currentTabId );
               this._.tabBarMode = false;
               this._.currentFocusIndex = -1;
               changeFocus( 1 );
               processed = 1;
            }
            // If user presses enter key in a text box, it implies clicking OK for the dialog.
            else if ( keystroke == 13 /*ENTER*/ ) {
               // Don't do that for a target that handles ENTER.
               var target = evt.data.getTarget();
               if ( !target.is( 'a', 'button', 'select', 'textarea' ) && ( !target.is( 'input' ) || target.$.type != 'button' ) ) {
                  button = this.getButton( 'ok' );
                  button && VED.tools.setTimeout( button.click, 0, button );
                  processed = 1;
               }
               stopPropagation = 1; // Always block the propagation (#4269)
            } else if ( keystroke == 27 /*ESC*/ ) {
               button = this.getButton( 'cancel' );

               // If there's a Cancel button, click it, else just fire the cancel event and hide the dialog.
               if ( button )
                  VED.tools.setTimeout( button.click, 0, button );
               else {
                  if ( this.fire( 'cancel', { hide: true } ).hide !== false )
                     this.hide();
               }
               stopPropagation = 1; // Always block the propagation (#4269)
            } else
               return;

            keypressHandler( evt );
         }

         function keypressHandler( evt ) {
            if ( processed )
               evt.data.preventDefault( 1 );
            else if ( stopPropagation )
               evt.data.stopPropagation();
         }

         var dialogElement = this._.element;

         editor.focusManager.add( dialogElement, 1 );

         // Add the dialog keyboard handlers.
         this.on( 'show', function() {
            dialogElement.on( 'keydown', keydownHandler, this );

            // Some browsers instead, don't cancel key events in the keydown, but in the
            // keypress. So we must do a longer trip in those cases. (#4531,#8985)
            if ( VED.env.opera || VED.env.gecko )
               dialogElement.on( 'keypress', keypressHandler, this );

         });
         this.on( 'hide', function() {
            dialogElement.removeListener( 'keydown', keydownHandler );
            if ( VED.env.opera || VED.env.gecko )
               dialogElement.removeListener( 'keypress', keypressHandler );

            // Reset fields state when closing dialog.
            iterContents( function( item ) {
               resetField.apply( item );
            });
         });
         this.on( 'iframeAdded', function( evt ) {
            var doc = new VED.dom.document( evt.data.iframe.$.contentWindow.document );
            doc.on( 'keydown', keydownHandler, this, null, 0 );
         });

         // Auto-focus logic in dialog.
         this.on( 'show', function() {
            // Setup tabIndex on showing the dialog instead of on loading
            // to allow dynamic tab order happen in dialog definition.
            setupFocus();

            if ( editor.config.dialog_startupFocusTab && me._.pageCount > 1 ) {
               me._.tabBarMode = true;
               me._.tabs[ me._.currentTabId ][ 0 ].focus();
            } else if ( !this._.hasFocus ) {
               this._.currentFocusIndex = -1;

               // Decide where to put the initial focus.
               if ( definition.onFocus ) {
                  var initialFocus = definition.onFocus.call( this );
                  // Focus the field that the user specified.
                  initialFocus && initialFocus.focus();
               }
               // Focus the first field in layout order.
               else
                  changeFocus( 1 );
            }
         }, this, null, 0xffffffff );

         // IE6 BUG: Text fields and text areas are only half-rendered the first time the dialog appears in IE6 (#2661).
         // This is still needed after [2708] and [2709] because text fields in hidden TR tags are still broken.
         if ( VED.env.ie6Compat ) {
            this.on( 'load', function( evt ) {
               var outer = this.getElement(),
                  inner = outer.getFirst();
               inner.remove();
               inner.appendTo( outer );
            }, this );
         }

         initDragAndDrop( this );
         initResizeHandles( this );

         // Insert the title.
         ( new VED.dom.text( definition.title, VED.document ) ).appendTo( this.parts.title );

         // Insert the tabs and contents.
         for ( i = 0; i < definition.contents.length; i++ ) {
            var page = definition.contents[ i ];
            page && this.addPage( page );
         }

         this.parts[ 'tabs' ].on( 'click', function( evt ) {
            var target = evt.data.getTarget();
            // If we aren't inside a tab, bail out.
            if ( target.hasClass( 'ved_dialog_tab' ) ) {
               // Get the ID of the tab, without the 'ved_' prefix and the unique number suffix.
               var id = target.$.id;
               this.selectPage( id.substring( 4, id.lastIndexOf( '_' ) ) );

               if ( this._.tabBarMode ) {
                  this._.tabBarMode = false;
                  this._.currentFocusIndex = -1;
                  changeFocus( 1 );
               }
               evt.data.preventDefault();
            }
         }, this );

         // Insert buttons.
         var buttonsHtml = [],
            buttons = VED.dialog._.uiElementBuilders.hbox.build( this, {
               type: 'hbox',
               className: 'ved_dialog_footer_buttons',
               widths: [],
               children: definition.buttons
            }, buttonsHtml ).getChild();
         this.parts.footer.setHtml( buttonsHtml.join( '' ) );

         for ( i = 0; i < buttons.length; i++ )
            this._.buttons[ buttons[ i ].id ] = buttons[ i ];
      },

      destroy: function() {
         this.hide();
         this._.element.remove();
      },

      resize: (function() {
         return function( width, height ) {
            if ( this._.contentSize && this._.contentSize.width == width && this._.contentSize.height == height )
               return;

            VED.dialog.fire( 'resize', {
               dialog: this,
               width: width,
               height: height
            }, this._.editor );

            this.fire( 'resize', {
               width: width,
               height: height
            }, this._.editor );

            var contents = this.parts.contents;
            contents.setStyles({
               width: width + 'px',
               height: height + 'px'
            });

            // Update dialog position when dimension get changed in RTL.
            if ( this._.editor.lang.dir == 'rtl' && this._.position )
               this._.position.x = VED.document.getWindow().getViewPaneSize().width - this._.contentSize.width - parseInt( this._.element.getFirst().getStyle( 'right' ), 10 );

            this._.contentSize = { width: width, height: height };
         };
      })(),

      getSize: function() {
         var element = this._.element.getFirst();
         return { width: element.$.offsetWidth || 0, height: element.$.offsetHeight || 0 };
      },

      move: function( x, y, save ) {
         // The dialog may be fixed positioned or absolute positioned. Ask the
         // browser what is the current situation first.
         var element = this._.element.getFirst(), rtl = this._.editor.lang.dir == 'rtl';
         var isFixed = element.getComputedStyle( 'position' ) == 'fixed';

         // (#8888) In some cases of a very small viewport, dialog is incorrectly
         // positioned in IE7. It also happens that it remains sticky and user cannot
         // scroll down/up to reveal dialog's content below/above the viewport; this is
         // cumbersome.
         // The only way to fix this is to move mouse out of the browser and
         // go back to see that dialog position is automagically fixed. No events,
         // no style change - pure magic. This is a IE7 rendering issue, which can be
         // fixed with dummy style redraw on each move.
         if ( VED.env.ie )
            element.setStyle( 'zoom', '100%' );

         if ( isFixed && this._.position && this._.position.x == x && this._.position.y == y )
            return;

         // Save the current position.
         this._.position = { x: x, y: y };

         // If not fixed positioned, add scroll position to the coordinates.
         if ( !isFixed ) {
            var scrollPosition = VED.document.getWindow().getScrollPosition();
            x += scrollPosition.x;
            y += scrollPosition.y;
         }

         // Translate coordinate for RTL.
         if ( rtl ) {
            var dialogSize = this.getSize(), viewPaneSize = VED.document.getWindow().getViewPaneSize();
            x = viewPaneSize.width - dialogSize.width - x;
         }

         var styles = { 'top': ( y > 0 ? y : 0 ) + 'px' };
         styles[ rtl ? 'right' : 'left' ] = ( x > 0 ? x : 0 ) + 'px';

         element.setStyles( styles );

         save && ( this._.moved = 1 );
      },

      getPosition: function() {
         return VED.tools.extend( {}, this._.position );
      },

      show: function() {
         // Insert the dialog's element to the root document.
         var element = this._.element;
         var definition = this.definition;
         if ( !( element.getParent() && element.getParent().equals( VED.document.getBody() ) ) )
            element.appendTo( VED.document.getBody() );
         else
            element.setStyle( 'display', 'block' );

         // FIREFOX BUG: Fix vanishing caret for Firefox 2 or Gecko 1.8.
         if ( VED.env.gecko && VED.env.version < 10900 ) {
            var dialogElement = this.parts.dialog;
            dialogElement.setStyle( 'position', 'absolute' );
            setTimeout( function() {
               dialogElement.setStyle( 'position', 'fixed' );
            }, 0 );
         }

         // First, set the dialog to an appropriate size.
         this.resize( this._.contentSize && this._.contentSize.width || definition.width || definition.minWidth, this._.contentSize && this._.contentSize.height || definition.height || definition.minHeight );

         // Reset all inputs back to their default value.
         this.reset();

         // Select the first tab by default.
         this.selectPage( this.definition.contents[ 0 ].id );

         // Set z-index.
         if ( VED.dialog._.currentZIndex === null )
            VED.dialog._.currentZIndex = this._.editor.config.baseFloatZIndex;
         this._.element.getFirst().setStyle( 'z-index', VED.dialog._.currentZIndex += 10 );

         // Maintain the dialog ordering and dialog cover.
         if ( VED.dialog._.currentTop === null ) {
            VED.dialog._.currentTop = this;
            this._.parentDialog = null;
            showCover( this._.editor );

         } else {
            this._.parentDialog = VED.dialog._.currentTop;
            var parentElement = this._.parentDialog.getElement().getFirst();
            parentElement.$.style.zIndex -= Math.floor( this._.editor.config.baseFloatZIndex / 2 );
            VED.dialog._.currentTop = this;
         }

         element.on( 'keydown', accessKeyDownHandler );
         element.on( VED.env.opera ? 'keypress' : 'keyup', accessKeyUpHandler );

         // Reset the hasFocus state.
         this._.hasFocus = false;

         VED.tools.setTimeout( function() {
            this.layout();
            resizeWithWindow( this );

            this.parts.dialog.setStyle( 'visibility', '' );

            // Execute onLoad for the first show.
            this.fireOnce( 'load', {} );
            VED.ui.fire( 'ready', this );

            this.fire( 'show', {} );
            this._.editor.fire( 'dialogShow', this );

            if ( !this._.parentDialog )
               this._.editor.focusManager.lock();

            // Save the initial values of the dialog.
            this.foreach( function( contentObj ) {
               contentObj.setInitValue && contentObj.setInitValue();
            });

         }, 100, this );
      },

      layout: function() {
         var el = this.parts.dialog;
         var dialogSize = this.getSize();
         var win = VED.document.getWindow(),
               viewSize = win.getViewPaneSize();

         var posX = ( viewSize.width - dialogSize.width ) / 2,
            posY = ( viewSize.height - dialogSize.height ) / 2;

         // Switch to absolute position when viewport is smaller than dialog size.
         if ( !VED.env.ie6Compat ) {
            if ( dialogSize.height + ( posY > 0 ? posY : 0 ) > viewSize.height ||
                   dialogSize.width + ( posX > 0 ? posX : 0 ) > viewSize.width )
               el.setStyle( 'position', 'absolute' );
            else
               el.setStyle( 'position', 'fixed' );
         }

         this.move( this._.moved ? this._.position.x : posX,
               this._.moved ? this._.position.y : posY );
      },

      foreach: function( fn ) {
         for ( var i in this._.contents ) {
            for ( var j in this._.contents[ i ] )
               fn.call( this, this._.contents[ i ][ j ] );
         }
         return this;
      },

      reset: (function() {
         var fn = function( widget ) {
               if ( widget.reset )
                  widget.reset( 1 );
            };
         return function() {
            this.foreach( fn );
            return this;
         };
      })(),

      setupContent: function() {
         var args = arguments;
         this.foreach( function( widget ) {
            if ( widget.setup )
               widget.setup.apply( widget, args );
         });
      },

      commitContent: function() {
         var args = arguments;
         this.foreach( function( widget ) {
            // Make sure IE triggers "change" event on last focused input before closing the dialog. (#7915)
            if ( VED.env.ie && this._.currentFocusIndex == widget.focusIndex )
               widget.getInputElement().$.blur();

            if ( widget.commit )
               widget.commit.apply( widget, args );
         });
      },

      hide: function() {
         if ( !this.parts.dialog.isVisible() )
            return;

         this.fire( 'hide', {} );
         this._.editor.fire( 'dialogHide', this );
         // Reset the tab page.
         this.selectPage( this._.tabIdList[ 0 ] );
         var element = this._.element;
         element.setStyle( 'display', 'none' );
         this.parts.dialog.setStyle( 'visibility', 'hidden' );
         // Unregister all access keys associated with this dialog.
         unregisterAccessKey( this );

         // Close any child(top) dialogs first.
         while ( VED.dialog._.currentTop != this )
            VED.dialog._.currentTop.hide();

         // Maintain dialog ordering and remove cover if needed.
         if ( !this._.parentDialog )
            hideCover( this._.editor );
         else {
            var parentElement = this._.parentDialog.getElement().getFirst();
            parentElement.setStyle( 'z-index', parseInt( parentElement.$.style.zIndex, 10 ) + Math.floor( this._.editor.config.baseFloatZIndex / 2 ) );
         }
         VED.dialog._.currentTop = this._.parentDialog;

         // Deduct or clear the z-index.
         if ( !this._.parentDialog ) {
            VED.dialog._.currentZIndex = null;

            // Remove access key handlers.
            element.removeListener( 'keydown', accessKeyDownHandler );
            element.removeListener( VED.env.opera ? 'keypress' : 'keyup', accessKeyUpHandler );

            var editor = this._.editor;
            editor.focus();

            // Give a while before unlock, waiting for focus to return to the editable. (#172)
            setTimeout( function() { editor.focusManager.unlock(); }, 0 );

         } else
            VED.dialog._.currentZIndex -= 10;

         delete this._.parentDialog;
         // Reset the initial values of the dialog.
         this.foreach( function( contentObj ) {
            contentObj.resetInitValue && contentObj.resetInitValue();
         });
      },

      addPage: function( contents ) {
         var pageHtml = [],
            titleHtml = contents.label ? ' title="' + VED.tools.htmlEncode( contents.label ) + '"' : '',
            elements = contents.elements,
            vbox = VED.dialog._.uiElementBuilders.vbox.build( this, {
               type: 'vbox',
               className: 'ved_dialog_page_contents',
               children: contents.elements,
               expand: !!contents.expand,
               padding: contents.padding,
               style: contents.style || 'width: 100%;'
            }, pageHtml );

         // Create the HTML for the tab and the content block.
         var page = VED.dom.element.createFromHtml( pageHtml.join( '' ) );
         page.setAttribute( 'role', 'tabpanel' );

         var env = VED.env;
         var tabId = 'ved_' + contents.id + '_' + VED.tools.getNextNumber(),
            tab = VED.dom.element.createFromHtml( [
               '<a class="ved_dialog_tab"',
                  ( this._.pageCount > 0 ? ' ved_last' : 'ved_first' ),
                  titleHtml,
                  ( !!contents.hidden ? ' style="display:none"' : '' ),
                  ' id="', tabId, '"',
                  env.gecko && env.version >= 10900 && !env.hc ? '' : ' href="javascript:void(0)"',
                  ' tabIndex="-1"',
                  ' hidefocus="true"',
                  ' role="tab">',
                     contents.label,
               '</a>'
               ].join( '' ) );

         page.setAttribute( 'aria-labelledby', tabId );

         // Take records for the tabs and elements created.
         this._.tabs[ contents.id ] = [ tab, page ];
         this._.tabIdList.push( contents.id );
         !contents.hidden && this._.pageCount++;
         this._.lastTab = tab;
         this.updateStyle();

         var contentMap = this._.contents[ contents.id ] = {},
            cursor,
            children = vbox.getChild();

         while ( ( cursor = children.shift() ) ) {
            contentMap[ cursor.id ] = cursor;
            if ( typeof( cursor.getChild ) == 'function' )
               children.push.apply( children, cursor.getChild() );
         }

         // Attach the DOM nodes.
         page.setAttribute( 'name', contents.id );
         page.appendTo( this.parts.contents );

         tab.unselectable();
         this.parts.tabs.append( tab );

         // Add access key handlers if access key is defined.
         if ( contents.accessKey ) {
            registerAccessKey( this, this, 'CTRL+' + contents.accessKey, tabAccessKeyDown, tabAccessKeyUp );
            this._.accessKeyMap[ 'CTRL+' + contents.accessKey ] = contents.id;
         }
      },

      selectPage: function( id ) {
         if ( this._.currentTabId == id )
            return;

         // Returning true means that the event has been canceled
         if ( this.fire( 'selectPage', { page: id, currentPage: this._.currentTabId } ) === true )
            return;

         // Hide the non-selected tabs and pages.
         for ( var i in this._.tabs ) {
            var tab = this._.tabs[ i ][ 0 ],
               page = this._.tabs[ i ][ 1 ];
            if ( i != id ) {
               tab.removeClass( 'ved_dialog_tab_selected' );
               page.hide();
            }
            page.setAttribute( 'aria-hidden', i != id );
         }

         var selected = this._.tabs[ id ];
         selected[ 0 ].addClass( 'ved_dialog_tab_selected' );

         // [IE] an invisible input[type='text'] will enlarge it's width
         // if it's value is long when it shows, so we clear it's value
         // before it shows and then recover it (#5649)
         if ( VED.env.ie6Compat || VED.env.ie7Compat ) {
            clearOrRecoverTextInputValue( selected[ 1 ] );
            selected[ 1 ].show();
            setTimeout( function() {
               clearOrRecoverTextInputValue( selected[ 1 ], 1 );
            }, 0 );
         } else
            selected[ 1 ].show();

         this._.currentTabId = id;
         this._.currentTabIndex = VED.tools.indexOf( this._.tabIdList, id );
      },

      updateStyle: function() {
         // If only a single page shown, a different style is used in the central pane.
         this.parts.dialog[ ( this._.pageCount === 1 ? 'add' : 'remove' ) + 'Class' ]( 'ved_single_page' );
      },

      hidePage: function( id ) {
         var tab = this._.tabs[ id ] && this._.tabs[ id ][ 0 ];
         if ( !tab || this._.pageCount == 1 || !tab.isVisible() )
            return;
         // Switch to other tab first when we're hiding the active tab.
         else if ( id == this._.currentTabId )
            this.selectPage( getPreviousVisibleTab.call( this ) );

         tab.hide();
         this._.pageCount--;
         this.updateStyle();
      },

      showPage: function( id ) {
         var tab = this._.tabs[ id ] && this._.tabs[ id ][ 0 ];
         if ( !tab )
            return;
         tab.show();
         this._.pageCount++;
         this.updateStyle();
      },

      getElement: function() {
         return this._.element;
      },

      getName: function() {
         return this._.name;
      },

      getContentElement: function( pageId, elementId ) {
         var page = this._.contents[ pageId ];
         return page && page[ elementId ];
      },

      getValueOf: function( pageId, elementId ) {
         return this.getContentElement( pageId, elementId ).getValue();
      },

      setValueOf: function( pageId, elementId, value ) {
         return this.getContentElement( pageId, elementId ).setValue( value );
      },

      getButton: function( id ) {
         return this._.buttons[ id ];
      },

      click: function( id ) {
         return this._.buttons[ id ].click();
      },

      disableButton: function( id ) {
         return this._.buttons[ id ].disable();
      },

      enableButton: function( id ) {
         return this._.buttons[ id ].enable();
      },

      getPageCount: function() {
         return this._.pageCount;
      },

      getParentEditor: function() {
         return this._.editor;
      },

      getSelectedElement: function() {
         return this.getParentEditor().getSelection().getSelectedElement();
      },

      addFocusable: function( element, index ) {
         if ( typeof index == 'undefined' ) {
            index = this._.focusList.length;
            this._.focusList.push( new Focusable( this, element, index ) );
         } else {
            this._.focusList.splice( index, 0, new Focusable( this, element, index ) );
            for ( var i = index + 1; i < this._.focusList.length; i++ )
               this._.focusList[ i ].focusIndex++;
         }
      }
   });

   VED.tools.extend( VED.dialog, {

      add: function( name, dialogDefinition ) {
         // Avoid path registration from multiple instances override definition.
         if ( !this._.dialogDefinitions[ name ] || typeof dialogDefinition == 'function' )
            this._.dialogDefinitions[ name ] = dialogDefinition;
      },

      exists: function( name ) {
         return !!this._.dialogDefinitions[ name ];
      },

      getCurrent: function() {
         return VED.dialog._.currentTop;
      },

      okButton: (function() {
         var retval = function( editor, override ) {
               override = override || {};
               return VED.tools.extend({
                  id: 'ok',
                  type: 'button',
                  label: editor.lang.common.ok,
                  'class': 'ved_dialog_ui_button_ok',
                  onClick: function( evt ) {
                     var dialog = evt.data.dialog;
                     if ( dialog.fire( 'ok', { hide: true } ).hide !== false )
                        dialog.hide();
                  }
               }, override, true );
            };
         retval.type = 'button';
         retval.override = function( override ) {
            return VED.tools.extend( function( editor ) {
               return retval( editor, override );
            }, { type: 'button' }, true );
         };
         return retval;
      })(),

      cancelButton: (function() {
         var retval = function( editor, override ) {
               override = override || {};
               return VED.tools.extend({
                  id: 'cancel',
                  type: 'button',
                  label: editor.lang.common.cancel,
                  'class': 'ved_dialog_ui_button_cancel',
                  onClick: function( evt ) {
                     var dialog = evt.data.dialog;
                     if ( dialog.fire( 'cancel', { hide: true } ).hide !== false )
                        dialog.hide();
                  }
               }, override, true );
            };
         retval.type = 'button';
         retval.override = function( override ) {
            return VED.tools.extend( function( editor ) {
               return retval( editor, override );
            }, { type: 'button' }, true );
         };
         return retval;
      })(),

      addUIElement: function( typeName, builder ) {
         this._.uiElementBuilders[ typeName ] = builder;
      }
   });

   VED.dialog._ = {
      uiElementBuilders: {},
      dialogDefinitions: {},
      currentTop: null,
      currentZIndex: null
   };

   // "Inherit" (copy actually) from VED.event.
   VED.tools.extend( VED.dialog, new VED.event );

   var defaultDialogDefinition = {
      resizable: VED.DIALOG_RESIZE_BOTH,
      minWidth: 600,
      minHeight: 400,
      buttons: [ VED.dialog.okButton, VED.dialog.cancelButton ]
   };

   // Tool function used to return an item from an array based on its id
   // property.
   var getById = function( array, id, recurse ) {
      for ( var i = 0, item;
      ( item = array[ i ] ); i++ ) {
         if ( item.id == id )
            return item;
         if ( recurse && item[ recurse ] ) {
            var retval = getById( item[ recurse ], id, recurse );
            if ( retval )
               return retval;
         }
      }
      return null;
   };

   // Tool function used to add an item into an array.
   var addById = function( array, newItem, nextSiblingId, recurse, nullIfNotFound ) {
      if ( nextSiblingId ) {
         for ( var i = 0, item;
         ( item = array[ i ] ); i++ ) {
            if ( item.id == nextSiblingId ) {
               array.splice( i, 0, newItem );
               return newItem;
            }

            if ( recurse && item[ recurse ] ) {
               var retval = addById( item[ recurse ], newItem, nextSiblingId, recurse, true );
               if ( retval )
                  return retval;
            }
         }

         if ( nullIfNotFound )
            return null;
      }

      array.push( newItem );
      return newItem;
   };

   // Tool function used to remove an item from an array based on its id.
   var removeById = function( array, id, recurse ) {
      for ( var i = 0, item;
      ( item = array[ i ] ); i++ ) {
         if ( item.id == id )
            return array.splice( i, 1 );
         if ( recurse && item[ recurse ] ) {
            var retval = removeById( item[ recurse ], id, recurse );
            if ( retval )
               return retval;
         }
      }
      return null;
   };

   var definitionObject = new Class({
      initialize: function( dialog, dialogDefinition ) {
         // TODO : Check if needed.
         this.dialog = dialog;

         // Transform the contents entries in contentObjects.
         var contents = dialogDefinition.contents;
         for ( var i = 0, content; ( content = contents[ i ] ); i++ )
            contents[ i ] = content && new contentObject( dialog, content );

         VED.tools.extend( this, dialogDefinition );
      },

      getContents: function( id ) {
         return getById( this.contents, id );
      },

      getButton: function( id ) {
         return getById( this.buttons, id );
      },

      addContents: function( contentDefinition, nextSiblingId ) {
         return addById( this.contents, contentDefinition, nextSiblingId );
      },

      addButton: function( buttonDefinition, nextSiblingId ) {
         return addById( this.buttons, buttonDefinition, nextSiblingId );
      },

      removeContents: function( id ) {
         removeById( this.contents, id );
      },

      removeButton: function( id ) {
         removeById( this.buttons, id );
      }
   });

   var contentObject = new Class({
      initialize: function( dialog, contentDefinition ) {
         this._ = {
            dialog: dialog
         };

         VED.tools.extend( this, contentDefinition );
      },

      get: function( id ) {
         return getById( this.elements, id, 'children' );
      },

      add: function( elementDefinition, nextSiblingId ) {
         return addById( this.elements, elementDefinition, nextSiblingId, 'children' );
      },

      remove: function( id ) {
         removeById( this.elements, id, 'children' );
      }
   });

   function initDragAndDrop( dialog ) {
      var lastCoords = null,
         abstractDialogCoords = null,
         element = dialog.getElement().getFirst(),
         editor = dialog.getParentEditor(),
         magnetDistance = editor.config.dialog_magnetDistance,
         margins = VED.skin.margins || [ 0, 0, 0, 0 ];

      if ( typeof magnetDistance == 'undefined' )
         magnetDistance = 20;

      function mouseMoveHandler( evt ) {
         var dialogSize = dialog.getSize(),
            viewPaneSize = VED.document.getWindow().getViewPaneSize(),
            x = evt.data.$.screenX,
            y = evt.data.$.screenY,
            dx = x - lastCoords.x,
            dy = y - lastCoords.y,
            realX, realY;

         lastCoords = { x: x, y: y };
         abstractDialogCoords.x += dx;
         abstractDialogCoords.y += dy;

         if ( abstractDialogCoords.x + margins[ 3 ] < magnetDistance )
            realX = -margins[ 3 ];
         else if ( abstractDialogCoords.x - margins[ 1 ] > viewPaneSize.width - dialogSize.width - magnetDistance )
            realX = viewPaneSize.width - dialogSize.width + ( editor.lang.dir == 'rtl' ? 0 : margins[ 1 ] );
         else
            realX = abstractDialogCoords.x;

         if ( abstractDialogCoords.y + margins[ 0 ] < magnetDistance )
            realY = -margins[ 0 ];
         else if ( abstractDialogCoords.y - margins[ 2 ] > viewPaneSize.height - dialogSize.height - magnetDistance )
            realY = viewPaneSize.height - dialogSize.height + margins[ 2 ];
         else
            realY = abstractDialogCoords.y;

         dialog.move( realX, realY, 1 );

         evt.data.preventDefault();
      }

      function mouseUpHandler( evt ) {
         VED.document.removeListener( 'mousemove', mouseMoveHandler );
         VED.document.removeListener( 'mouseup', mouseUpHandler );

         if ( VED.env.ie6Compat ) {
            var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
            coverDoc.removeListener( 'mousemove', mouseMoveHandler );
            coverDoc.removeListener( 'mouseup', mouseUpHandler );
         }
      }

      dialog.parts.title.on( 'mousedown', function( evt ) {
         lastCoords = { x: evt.data.$.screenX, y: evt.data.$.screenY };

         VED.document.on( 'mousemove', mouseMoveHandler );
         VED.document.on( 'mouseup', mouseUpHandler );
         abstractDialogCoords = dialog.getPosition();

         if ( VED.env.ie6Compat ) {
            var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
            coverDoc.on( 'mousemove', mouseMoveHandler );
            coverDoc.on( 'mouseup', mouseUpHandler );
         }

         evt.data.preventDefault();
      }, dialog );
   }

   function initResizeHandles( dialog ) {
      var def = dialog.definition,
         resizable = def.resizable;

      if ( resizable == VED.DIALOG_RESIZE_NONE )
         return;

      var editor = dialog.getParentEditor();
      var wrapperWidth, wrapperHeight, viewSize, origin, startSize, dialogCover;

      var mouseDownFn = VED.tools.addFunction( function( $event ) {
         startSize = dialog.getSize();

         var content = dialog.parts.contents,
            iframeDialog = content.$.getElementsByTagName( 'iframe' ).length;

         // Shim to help capturing "mousemove" over iframe.
         if ( iframeDialog ) {
            dialogCover = VED.dom.element.createFromHtml( '<div class="ved_dialog_resize_cover" style="height: 100%; position: absolute; width: 100%;"></div>' );
            content.append( dialogCover );
         }

         // Calculate the offset between content and chrome size.
         wrapperHeight = startSize.height - dialog.parts.contents.getSize( 'height', !( VED.env.gecko || VED.env.opera || VED.env.ie && VED.env.quirks ) );
         wrapperWidth = startSize.width - dialog.parts.contents.getSize( 'width', 1 );

         origin = { x: $event.screenX, y: $event.screenY };

         viewSize = VED.document.getWindow().getViewPaneSize();

         VED.document.on( 'mousemove', mouseMoveHandler );
         VED.document.on( 'mouseup', mouseUpHandler );

         if ( VED.env.ie6Compat ) {
            var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
            coverDoc.on( 'mousemove', mouseMoveHandler );
            coverDoc.on( 'mouseup', mouseUpHandler );
         }

         $event.preventDefault && $event.preventDefault();
      });

      // Prepend the grip to the dialog.
      dialog.on( 'load', function() {
         var direction = '';
         if ( resizable == VED.DIALOG_RESIZE_WIDTH )
            direction = ' ved_resizer_horizontal';
         else if ( resizable == VED.DIALOG_RESIZE_HEIGHT )
            direction = ' ved_resizer_vertical';
         var resizer = VED.dom.element.createFromHtml(
            '<div' +
            ' class="ved_resizer' + direction + ' ved_resizer_' + editor.lang.dir + '"' +
            ' title="' + VED.tools.htmlEncode( editor.lang.common.resize ) + '"' +
            ' onmousedown="VED.tools.callFunction(' + mouseDownFn + ', event )">' +
            // BLACK LOWER RIGHT TRIANGLE (ltr)
            // BLACK LOWER LEFT TRIANGLE (rtl)
            ( editor.lang.dir == 'ltr' ? '\u25E2' : '\u25E3' ) +
            '</div>' );
         dialog.parts.footer.append( resizer, 1 );
      });
      editor.on( 'destroy', function() {
         VED.tools.removeFunction( mouseDownFn );
      });

      function mouseMoveHandler( evt ) {
         var rtl = editor.lang.dir == 'rtl',
            dx = ( evt.data.$.screenX - origin.x ) * ( rtl ? -1 : 1 ),
            dy = evt.data.$.screenY - origin.y,
            width = startSize.width,
            height = startSize.height,
            internalWidth = width + dx * ( dialog._.moved ? 1 : 2 ),
            internalHeight = height + dy * ( dialog._.moved ? 1 : 2 ),
            element = dialog._.element.getFirst(),
            right = rtl && element.getComputedStyle( 'right' ),
            position = dialog.getPosition();

         if ( position.y + internalHeight > viewSize.height )
            internalHeight = viewSize.height - position.y;

         if ( ( rtl ? right : position.x ) + internalWidth > viewSize.width )
            internalWidth = viewSize.width - ( rtl ? right : position.x );

         // Make sure the dialog will not be resized to the wrong side when it's in the leftmost position for RTL.
         if ( ( resizable == VED.DIALOG_RESIZE_WIDTH || resizable == VED.DIALOG_RESIZE_BOTH ) )
            width = Math.max( def.minWidth || 0, internalWidth - wrapperWidth );

         if ( resizable == VED.DIALOG_RESIZE_HEIGHT || resizable == VED.DIALOG_RESIZE_BOTH )
            height = Math.max( def.minHeight || 0, internalHeight - wrapperHeight );

         dialog.resize( width, height );

         if ( !dialog._.moved )
            dialog.layout();

         evt.data.preventDefault();
      }

      function mouseUpHandler() {
         VED.document.removeListener( 'mouseup', mouseUpHandler );
         VED.document.removeListener( 'mousemove', mouseMoveHandler );

         if ( dialogCover ) {
            dialogCover.remove();
            dialogCover = null;
         }

         if ( VED.env.ie6Compat ) {
            var coverDoc = currentCover.getChild( 0 ).getFrameDocument();
            coverDoc.removeListener( 'mouseup', mouseUpHandler );
            coverDoc.removeListener( 'mousemove', mouseMoveHandler );
         }
      }
   }

   var resizeCover;
   // Caching resuable covers and allowing only one cover
   // on screen.
   var covers = {},
      currentCover;

   function cancelEvent( ev ) {
      ev.data.preventDefault( 1 );
   }

   function showCover( editor ) {
      var win = VED.document.getWindow();
      var config = editor.config,
         backgroundColorStyle = config.dialog_backgroundCoverColor || 'white',
         backgroundCoverOpacity = config.dialog_backgroundCoverOpacity,
         baseFloatZIndex = config.baseFloatZIndex,
         coverKey = VED.tools.genKey( backgroundColorStyle, backgroundCoverOpacity, baseFloatZIndex ),
         coverElement = covers[ coverKey ];

      if ( !coverElement ) {
         var html = [
            '<div tabIndex="-1" style="position: ', ( VED.env.ie6Compat ? 'absolute' : 'fixed' ),
            '; z-index: ', baseFloatZIndex,
            '; top: 0px; left: 0px; ',
            ( !VED.env.ie6Compat ? 'background-color: ' + backgroundColorStyle : '' ),
            '" class="ved_dialog_background_cover">'
            ];

         if ( VED.env.ie6Compat ) {
            // Support for custom document.domain in IE.
            var isCustomDomain = VED.env.isCustomDomain(),
               iframeHtml = '<html><body style=\\\'background-color:' + backgroundColorStyle + ';\\\'></body></html>';

            html.push( '<iframe' +
               ' hidefocus="true"' +
               ' frameborder="0"' +
               ' id="ved_dialog_background_iframe"' +
               ' src="javascript:' );

            html.push( 'void((function(){' +
               'document.open();' +
               ( isCustomDomain ? 'document.domain=\'' + document.domain + '\';' : '' ) +
               'document.write( \'' + iframeHtml + '\' );' +
               'document.close();' +
               '})())' );

            html.push( '"' +
               ' style="' +
                  'position:absolute;' +
                  'left:0;' +
                  'top:0;' +
                  'width:100%;' +
                  'height: 100%;' +
                  'filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0)">' +
               '</iframe>' );
         }

         html.push( '</div>' );

         coverElement = VED.dom.element.createFromHtml( html.join( '' ) );
         coverElement.setOpacity( backgroundCoverOpacity != undefined ? backgroundCoverOpacity : 0.5 );

         coverElement.on( 'keydown', cancelEvent );
         coverElement.on( 'keypress', cancelEvent );
         coverElement.on( 'keyup', cancelEvent );

         coverElement.appendTo( VED.document.getBody() );
         covers[ coverKey ] = coverElement;
      } else
         coverElement.show();

      // Makes the dialog cover a focus holder as well.
      editor.focusManager.add( coverElement );

      currentCover = coverElement;
      var resizeFunc = function() {
            var size = win.getViewPaneSize();
            coverElement.setStyles({
               width: size.width + 'px',
               height: size.height + 'px'
            });
         };

      var scrollFunc = function() {
            var pos = win.getScrollPosition(),
               cursor = VED.dialog._.currentTop;
            coverElement.setStyles({
               left: pos.x + 'px',
               top: pos.y + 'px'
            });

            if ( cursor ) {
               do {
                  var dialogPos = cursor.getPosition();
                  cursor.move( dialogPos.x, dialogPos.y );
               } while ( ( cursor = cursor._.parentDialog ) );
            }
         };

      resizeCover = resizeFunc;
      win.on( 'resize', resizeFunc );
      resizeFunc();
      // Using Safari/Mac, focus must be kept where it is (#7027)
      if ( !( VED.env.mac && VED.env.webkit ) )
         coverElement.focus();

      if ( VED.env.ie6Compat ) {
         // IE BUG: win.$.onscroll assignment doesn't work.. it must be window.onscroll.
         // So we need to invent a really funny way to make it work.
         var myScrollHandler = function() {
               scrollFunc();
               arguments.callee.prevScrollHandler.apply( this, arguments );
            };
         win.$.setTimeout( function() {
            myScrollHandler.prevScrollHandler = window.onscroll ||
            function() {};
            window.onscroll = myScrollHandler;
         }, 0 );
         scrollFunc();
      }
   }

   function hideCover( editor ) {
      if ( !currentCover )
         return;

      editor.focusManager.remove( currentCover );
      var win = VED.document.getWindow();
      currentCover.hide();
      win.removeListener( 'resize', resizeCover );

      if ( VED.env.ie6Compat ) {
         win.$.setTimeout( function() {
            var prevScrollHandler = window.onscroll && window.onscroll.prevScrollHandler;
            window.onscroll = prevScrollHandler || null;
         }, 0 );
      }
      resizeCover = null;
   }

   function removeCovers() {
      for ( var coverId in covers )
         covers[ coverId ].remove();
      covers = {};
   }

   var accessKeyProcessors = {};

   var accessKeyDownHandler = function( evt ) {
      var ctrl = evt.data.$.ctrlKey || evt.data.$.metaKey,
         alt = evt.data.$.altKey,
         shift = evt.data.$.shiftKey,
         key = String.fromCharCode( evt.data.$.keyCode ),
         keyProcessor = accessKeyProcessors[ ( ctrl ? 'CTRL+' : '' ) + ( alt ? 'ALT+' : '' ) + ( shift ? 'SHIFT+' : '' ) + key ];

      if ( !keyProcessor || !keyProcessor.length )
         return;

      keyProcessor = keyProcessor[ keyProcessor.length - 1 ];
      keyProcessor.keydown && keyProcessor.keydown.call( keyProcessor.uiElement, keyProcessor.dialog, keyProcessor.key );
      evt.data.preventDefault();
   };

   var accessKeyUpHandler = function( evt ) {
      var ctrl = evt.data.$.ctrlKey || evt.data.$.metaKey,
         alt = evt.data.$.altKey,
         shift = evt.data.$.shiftKey,
         key = String.fromCharCode( evt.data.$.keyCode ),
         keyProcessor = accessKeyProcessors[ ( ctrl ? 'CTRL+' : '' ) + ( alt ? 'ALT+' : '' ) + ( shift ? 'SHIFT+' : '' ) + key ];

      if ( !keyProcessor || !keyProcessor.length )
         return;

      keyProcessor = keyProcessor[ keyProcessor.length - 1 ];
      if ( keyProcessor.keyup ) {
         keyProcessor.keyup.call( keyProcessor.uiElement, keyProcessor.dialog, keyProcessor.key );
         evt.data.preventDefault();
      }
   };

   var registerAccessKey = function( uiElement, dialog, key, downFunc, upFunc ) {
      var procList = accessKeyProcessors[ key ] || ( accessKeyProcessors[ key ] = [] );
      procList.push({
         uiElement: uiElement,
         dialog: dialog,
         key: key,
         keyup: upFunc || uiElement.accessKeyUp,
         keydown: downFunc || uiElement.accessKeyDown
      });
   };

   var unregisterAccessKey = function( obj ) {
      for ( var i in accessKeyProcessors ) {
         var list = accessKeyProcessors[ i ];
         for ( var j = list.length - 1; j >= 0; j-- ) {
            if ( list[ j ].dialog == obj || list[ j ].uiElement == obj )
               list.splice( j, 1 );
         }
         if ( list.length === 0 )
            delete accessKeyProcessors[ i ];
      }
   };

   var tabAccessKeyUp = function( dialog, key ) {
      if ( dialog._.accessKeyMap[ key ] )
         dialog.selectPage( dialog._.accessKeyMap[ key ] );
   };

   var tabAccessKeyDown = function( dialog, key ) {};

   (function() {
      VED.ui.dialog = {

         uiElement: function( dialog, elementDefinition, htmlList, nodeNameArg, stylesArg, attributesArg, contentsArg ) {
            if ( arguments.length < 4 )
               return;

            var nodeName = ( nodeNameArg.call ? nodeNameArg( elementDefinition ) : nodeNameArg ) || 'div',
               html = [ '<', nodeName, ' ' ],
               styles = ( stylesArg && stylesArg.call ? stylesArg( elementDefinition ) : stylesArg ) || {},
               attributes = ( attributesArg && attributesArg.call ? attributesArg( elementDefinition ) : attributesArg ) || {},
               innerHTML = ( contentsArg && contentsArg.call ? contentsArg.call( this, dialog, elementDefinition ) : contentsArg ) || '',
               domId = this.domId = attributes.id || VED.tools.getNextId() + '_uiElement',
               id = this.id = elementDefinition.id,
               i;

            // Set the id, a unique id is required for getElement() to work.
            attributes.id = domId;

            // Set the type and definition CSS class names.
            var classes = {};
            if ( elementDefinition.type )
               classes[ 'ved_dialog_ui_' + elementDefinition.type ] = 1;
            if ( elementDefinition.className )
               classes[ elementDefinition.className ] = 1;
            if ( elementDefinition.disabled )
               classes[ 'ved_disabled' ] = 1;

            var attributeClasses = ( attributes[ 'class' ] && attributes[ 'class' ].split ) ? attributes[ 'class' ].split( ' ' ) : [];
            for ( i = 0; i < attributeClasses.length; i++ ) {
               if ( attributeClasses[ i ] )
                  classes[ attributeClasses[ i ] ] = 1;
            }
            var finalClasses = [];
            for ( i in classes )
               finalClasses.push( i );
            attributes[ 'class' ] = finalClasses.join( ' ' );

            // Set the popup tooltop.
            if ( elementDefinition.title )
               attributes.title = elementDefinition.title;

            // Write the inline CSS styles.
            var styleStr = ( elementDefinition.style || '' ).split( ';' );

            // Element alignment support.
            if ( elementDefinition.align ) {
               var align = elementDefinition.align;
               styles[ 'margin-left' ] = align == 'left' ? 0 : 'auto';
               styles[ 'margin-right' ] = align == 'right' ? 0 : 'auto';
            }

            for ( i in styles )
               styleStr.push( i + ':' + styles[ i ] );
            if ( elementDefinition.hidden )
               styleStr.push( 'display:none' );
            for ( i = styleStr.length - 1; i >= 0; i-- ) {
               if ( styleStr[ i ] === '' )
                  styleStr.splice( i, 1 );
            }
            if ( styleStr.length > 0 )
               attributes.style = ( attributes.style ? ( attributes.style + '; ' ) : '' ) + styleStr.join( '; ' );

            // Write the attributes.
            for ( i in attributes )
               html.push( i + '="' + VED.tools.htmlEncode( attributes[ i ] ) + '" ' );

            // Write the content HTML.
            html.push( '>', innerHTML, '</', nodeName, '>' );

            // Add contents to the parent HTML array.
            htmlList.push( html.join( '' ) );

            ( this._ || ( this._ = {} ) ).dialog = dialog;

            // Override isChanged if it is defined in element definition.
            if ( typeof( elementDefinition.isChanged ) == 'boolean' )
               this.isChanged = function() {
               return elementDefinition.isChanged;
            };
            if ( typeof( elementDefinition.isChanged ) == 'function' )
               this.isChanged = elementDefinition.isChanged;

            // Overload 'get(set)Value' on definition.
            if ( typeof( elementDefinition.setValue ) == 'function' ) {
               this.setValue = VED.tools.override( this.setValue, function( org ) {
                  return function( val ) {
                     org.call( this, elementDefinition.setValue.call( this, val ) );
                  };
               });
            }

            if ( typeof( elementDefinition.getValue ) == 'function' ) {
               this.getValue = VED.tools.override( this.getValue, function( org ) {
                  return function() {
                     return elementDefinition.getValue.call( this, org.call( this ) );
                  };
               });
            }

            // Add events.
            // VED.event.implementOn( this );
            VED.tools.extend( this, new VED.event );

            this.registerEvents( elementDefinition );
            if ( this.accessKeyUp && this.accessKeyDown && elementDefinition.accessKey )
               registerAccessKey( this, dialog, 'CTRL+' + elementDefinition.accessKey );

            var me = this;
            dialog.on( 'load', function() {
               var input = me.getInputElement();
               if ( input ) {
                  var focusClass = me.type in { 'checkbox':1,'ratio':1 } && VED.env.ie && VED.env.version < 8 ? 'ved_dialog_ui_focused' : '';
                  input.on( 'focus', function() {
                     dialog._.tabBarMode = false;
                     dialog._.hasFocus = true;
                     me.fire( 'focus' );
                     focusClass && this.addClass( focusClass );

                  });

                  input.on( 'blur', function() {
                     me.fire( 'blur' );
                     focusClass && this.removeClass( focusClass );
                  });
               }
            });

            // Register the object as a tab focus if it can be included.
            if ( this.keyboardFocusable ) {
               this.tabIndex = elementDefinition.tabIndex || 0;

               this.focusIndex = dialog._.focusList.push( this ) - 1;
               this.on( 'focus', function() {
                  dialog._.currentFocusIndex = me.focusIndex;
               });
            }

            // Completes this object with everything we have in the
            // definition.
            VED.tools.extend( this, elementDefinition );
         },

         hbox: function( dialog, childObjList, childHtmlList, htmlList, elementDefinition ) {
            if ( arguments.length < 4 )
               return;

            this._ || ( this._ = {} );

            var children = this._.children = childObjList,
               widths = elementDefinition && elementDefinition.widths || null,
               height = elementDefinition && elementDefinition.height || null,
               styles = {},
               i;
            /** @ignore */
            var innerHTML = function() {
                  var html = [ '<tbody><tr class="ved_dialog_ui_hbox">' ];
                  for ( i = 0; i < childHtmlList.length; i++ ) {
                     var className = 'ved_dialog_ui_hbox_child',
                        styles = [];
                     if ( i === 0 )
                        className = 'ved_dialog_ui_hbox_first';
                     if ( i == childHtmlList.length - 1 )
                        className = 'ved_dialog_ui_hbox_last';
                     html.push( '<td class="', className, '" role="presentation" ' );
                     if ( widths ) {
                        if ( widths[ i ] )
                           styles.push( 'width:' + cssLength( widths[ i ] ) );
                     } else
                        styles.push( 'width:' + Math.floor( 100 / childHtmlList.length ) + '%' );
                     if ( height )
                        styles.push( 'height:' + cssLength( height ) );
                     if ( elementDefinition && elementDefinition.padding != undefined )
                        styles.push( 'padding:' + cssLength( elementDefinition.padding ) );
                     // In IE Quirks alignment has to be done on table cells. (#7324)
                     if ( VED.env.ie && VED.env.quirks && children[ i ].align )
                        styles.push( 'text-align:' + children[ i ].align );
                     if ( styles.length > 0 )
                        html.push( 'style="' + styles.join( '; ' ) + '" ' );
                     html.push( '>', childHtmlList[ i ], '</td>' );
                  }
                  html.push( '</tr></tbody>' );
                  return html.join( '' );
               };

            var attribs = { role: 'presentation' };
            elementDefinition && elementDefinition.align && ( attribs.align = elementDefinition.align );

            VED.ui.dialog.uiElement.call( this, dialog, elementDefinition || { type: 'hbox' }, htmlList, 'table', styles, attribs, innerHTML );
         },

         vbox: function( dialog, childObjList, childHtmlList, htmlList, elementDefinition ) {
            if ( arguments.length < 3 )
               return;

            this._ || ( this._ = {} );

            var children = this._.children = childObjList,
               width = elementDefinition && elementDefinition.width || null,
               heights = elementDefinition && elementDefinition.heights || null;
            /** @ignore */
            var innerHTML = function() {
                  var html = [ '<table role="presentation" cellspacing="0" border="0" ' ];
                  html.push( 'style="' );
                  if ( elementDefinition && elementDefinition.expand )
                     html.push( 'height:100%;' );
                  html.push( 'width:' + cssLength( width || '100%' ), ';' );
                  html.push( '"' );
                  html.push( 'align="', VED.tools.htmlEncode(
                  ( elementDefinition && elementDefinition.align ) || ( dialog.getParentEditor().lang.dir == 'ltr' ? 'left' : 'right' ) ), '" ' );

                  html.push( '><tbody>' );
                  for ( var i = 0; i < childHtmlList.length; i++ ) {
                     var styles = [];
                     html.push( '<tr><td role="presentation" ' );
                     if ( width )
                        styles.push( 'width:' + cssLength( width || '100%' ) );
                     if ( heights )
                        styles.push( 'height:' + cssLength( heights[ i ] ) );
                     else if ( elementDefinition && elementDefinition.expand )
                        styles.push( 'height:' + Math.floor( 100 / childHtmlList.length ) + '%' );
                     if ( elementDefinition && elementDefinition.padding != undefined )
                        styles.push( 'padding:' + cssLength( elementDefinition.padding ) );
                     // In IE Quirks alignment has to be done on table cells. (#7324)
                     if ( VED.env.ie && VED.env.quirks && children[ i ].align )
                        styles.push( 'text-align:' + children[ i ].align );
                     if ( styles.length > 0 )
                        html.push( 'style="', styles.join( '; ' ), '" ' );
                     html.push( ' class="ved_dialog_ui_vbox_child">', childHtmlList[ i ], '</td></tr>' );
                  }
                  html.push( '</tbody></table>' );
                  return html.join( '' );
               };
            VED.ui.dialog.uiElement.call( this, dialog, elementDefinition || { type: 'vbox' }, htmlList, 'div', null, { role: 'presentation' }, innerHTML );
         }
      };
   })();

   VED.ui.dialog.uiElement.prototype = {

      getElement: function() {
         return VED.document.getById( this.domId );
      },

      getInputElement: function() {
         return this.getElement();
      },

      getDialog: function() {
         return this._.dialog;
      },

      setValue: function( value, noChangeEvent ) {
         this.getInputElement().setValue( value );
         !noChangeEvent && this.fire( 'change', { value: value } );
         return this;
      },

      getValue: function() {
         return this.getInputElement().getValue();
      },

      isChanged: function() {
         // Override in input classes.
         return false;
      },

      selectParentTab: function() {
         var element = this.getInputElement(),
            cursor = element,
            tabId;
         while ( ( cursor = cursor.getParent() ) && cursor.$.className.search( 'ved_dialog_page_contents' ) == -1 ) {
            /*jsl:pass*/
         }

         // Some widgets don't have parent tabs (e.g. OK and Cancel buttons).
         if ( !cursor )
            return this;

         tabId = cursor.getAttribute( 'name' );
         // Avoid duplicate select.
         if ( this._.dialog._.currentTabId != tabId )
            this._.dialog.selectPage( tabId );
         return this;
      },

      focus: function() {
         this.selectParentTab().getInputElement().focus();
         return this;
      },

      registerEvents: function( definition ) {
         var regex = /^on([A-Z]\w+)/,
            match;

         var registerDomEvent = function( uiElement, dialog, eventName, func ) {
               dialog.on( 'load', function() {
                  uiElement.getInputElement().on( eventName, func, uiElement );
               });
            };

         for ( var i in definition ) {
            if ( !( match = i.match( regex ) ) )
               continue;
            if ( this.eventProcessors[ i ] )
               this.eventProcessors[ i ].call( this, this._.dialog, definition[ i ] );
            else
               registerDomEvent( this, this._.dialog, match[ 1 ].toLowerCase(), definition[ i ] );
         }

         return this;
      },

      eventProcessors: {
         onLoad: function( dialog, func ) {
            dialog.on( 'load', func, this );
         },

         onShow: function( dialog, func ) {
            dialog.on( 'show', func, this );
         },

         onHide: function( dialog, func ) {
            dialog.on( 'hide', func, this );
         }
      },

      accessKeyDown: function( dialog, key ) {
         this.focus();
      },

      accessKeyUp: function( dialog, key ) {},

      disable: function() {
         var element = this.getElement(),
            input = this.getInputElement();
         input.setAttribute( 'disabled', 'true' );
         element.addClass( 'ved_disabled' );
      },

      enable: function() {
         var element = this.getElement(),
            input = this.getInputElement();
         input.removeAttribute( 'disabled' );
         element.removeClass( 'ved_disabled' );
      },

      isEnabled: function() {
         return !this.getElement().hasClass( 'ved_disabled' );
      },

      isVisible: function() {
         return this.getInputElement().isVisible();
      },

      isFocusable: function() {
         if ( !this.isEnabled() || !this.isVisible() )
            return false;
         return true;
      }
   };

   VED.ui.dialog.hbox.prototype = VED.tools.extend( new VED.ui.dialog.uiElement, {
      getChild: function( indices ) {
         // If no arguments, return a clone of the children array.
         if ( arguments.length < 1 )
            return this._.children.concat();

         // If indices isn't array, make it one.
         if ( !indices.splice )
            indices = [ indices ];

         // Retrieve the child element according to tree position.
         if ( indices.length < 2 )
            return this._.children[ indices[ 0 ] ];
         else
            return ( this._.children[ indices[ 0 ] ] && this._.children[ indices[ 0 ] ].getChild ) ? this._.children[ indices[ 0 ] ].getChild( indices.slice( 1, indices.length ) ) : null;
      }
   }, true );

   VED.ui.dialog.vbox.prototype = new VED.ui.dialog.hbox();

   (function() {
      var commonBuilder = {
         build: function( dialog, elementDefinition, output ) {
            var children = elementDefinition.children,
               child,
               childHtmlList = [],
               childObjList = [];
            for ( var i = 0;
            ( i < children.length && ( child = children[ i ] ) ); i++ ) {
               var childHtml = [];
               childHtmlList.push( childHtml );
               childObjList.push( VED.dialog._.uiElementBuilders[ child.type ].build( dialog, child, childHtml ) );
            }
            return new VED.ui.dialog[ elementDefinition.type ]( dialog, childObjList, childHtmlList, output, elementDefinition );
         }
      };

      VED.dialog.addUIElement( 'hbox', commonBuilder );
      VED.dialog.addUIElement( 'vbox', commonBuilder );
   })();

   VED.dialogCommand = function( dialogName, ext ) {
		this.dialogName = dialogName;
		VED.tools.extend( this, ext, true );
	};

	VED.dialogCommand.prototype = {
		exec: function( editor ) {
			// Special treatment for Opera. (#8031)
			VED.env.opera ? VED.tools.setTimeout( function() {
				editor.openDialog( this.dialogName );
			}, 0, this ) : editor.openDialog( this.dialogName );
		},

		// Dialog commands just open a dialog ui, thus require no undo logic,
		// undo support should dedicate to specific dialog implementation.
		canUndo: false,

		editorFocus: VED.env.ie || VED.env.webkit
	};

   (function() {
      var notEmptyRegex = /^([a]|[^a])+$/,
         integerRegex = /^\d*$/,
         numberRegex = /^\d*(?:\.\d+)?$/,
         htmlLengthRegex = /^(((\d*(\.\d+))|(\d*))(px|\%)?)?$/,
         cssLengthRegex = /^(((\d*(\.\d+))|(\d*))(px|em|ex|in|cm|mm|pt|pc|\%)?)?$/i,
         inlineStyleRegex = /^(\s*[\w-]+\s*:\s*[^:;]+(?:;|$))*$/;

      VED.VALIDATE_OR = 1;
      VED.VALIDATE_AND = 2;

      VED.dialog.validate = {
         functions: function() {
            var args = arguments;
            return function() {
               /**
                * It's important for validate functions to be able to accept the value
                * as argument in addition to this.getValue(), so that it is possible to
                * combine validate functions together to make more sophisticated
                * validators.
                */
               var value = this && this.getValue ? this.getValue() : args[ 0 ];

               var msg = undefined,
                  relation = VED.VALIDATE_AND,
                  functions = [],
                  i;

               for ( i = 0; i < args.length; i++ ) {
                  if ( typeof( args[ i ] ) == 'function' )
                     functions.push( args[ i ] );
                  else
                     break;
               }

               if ( i < args.length && typeof( args[ i ] ) == 'string' ) {
                  msg = args[ i ];
                  i++;
               }

               if ( i < args.length && typeof( args[ i ] ) == 'number' )
                  relation = args[ i ];

               var passed = ( relation == VED.VALIDATE_AND ? true : false );
               for ( i = 0; i < functions.length; i++ ) {
                  if ( relation == VED.VALIDATE_AND )
                     passed = passed && functions[ i ]( value );
                  else
                     passed = passed || functions[ i ]( value );
               }

               return !passed ? msg : true;
            };
         },

         regex: function( regex, msg ) {
            /*
             * Can be greatly shortened by deriving from functions validator if code size
             * turns out to be more important than performance.
             */
            return function() {
               var value = this && this.getValue ? this.getValue() : arguments[ 0 ];
               return !regex.test( value ) ? msg : true;
            };
         },

         notEmpty: function( msg ) {
            return this.regex( notEmptyRegex, msg );
         },

         integer: function( msg ) {
            return this.regex( integerRegex, msg );
         },

         'number': function( msg ) {
            return this.regex( numberRegex, msg );
         },

         'cssLength': function( msg ) {
            return this.functions( function( val ) {
               return cssLengthRegex.test( val.trim() );
            }, msg );
         },

         'htmlLength': function( msg ) {
            return this.functions( function( val ) {
               return htmlLengthRegex.test( val.trim() );
            }, msg );
         },

         'inlineStyle': function( msg ) {
            return this.functions( function( val ) {
               return inlineStyleRegex.test( val.trim() );
            }, msg );
         },

         equals: function( value, msg ) {
            return this.functions( function( val ) {
               return val == value;
            }, msg );
         },

         notEqual: function( value, msg ) {
            return this.functions( function( val ) {
               return val != value;
            }, msg );
         }
      };

      VED.on( 'instanceDestroyed', function( evt ) {
         // Remove dialog cover on last instance destroy.
         if ( VED.tools.isEmpty( VED.instances ) ) {
            var currentTopDialog;
            while ( ( currentTopDialog = VED.dialog._.currentTop ) )
               currentTopDialog.hide();
            removeCovers();
         }

         var dialogs = evt.editor._.storedDialogs;
         for ( var name in dialogs )
            dialogs[ name ].destroy();

      });

   })();

   // Extend the VED.editor class with dialog specific functions.
   VED.editor.implement({
      openDialog: function( dialogName, callback ) {
         var dialog = null, dialogDefinitions = VED.dialog._.dialogDefinitions[ dialogName ];

         if ( VED.dialog._.currentTop === null )
            showCover( this );

         // If the dialogDefinition is already loaded, open it immediately.
         if ( typeof dialogDefinitions == 'function' ) {
            var storedDialogs = this._.storedDialogs || ( this._.storedDialogs = {} );

            dialog = storedDialogs[ dialogName ] || ( storedDialogs[ dialogName ] = new VED.dialog( this, dialogName ) );

            callback && callback.call( dialog, dialog );
            dialog.show();

         } else if ( dialogDefinitions == 'failed' ) {
            hideCover( this );
            throw new Error( '[VED.dialog.openDialog] Dialog "' + dialogName + '" failed when loading definition.' );
         } else if ( typeof dialogDefinitions == 'string' ) {

            VED.scriptLoader.load( VED.getUrl( dialogDefinitions ),
               function() {
                  var dialogDefinition = VED.dialog._.dialogDefinitions[ dialogName ];
                  // In case of plugin error, mark it as loading failed.
                  if ( typeof dialogDefinition != 'function' )
                     VED.dialog._.dialogDefinitions[ dialogName ] = 'failed';

                  this.openDialog( dialogName, callback );
               }, this, 0, 1 );
         }

         VED.skin.loadPart( 'dialog' );

         return dialog;
      }
   });
})();

VED.plugins.add( 'dialog', {
   requires: 'dialogui',
   init: function( editor ) {
      editor.on( 'contentDom', function() {
         var editable = editor.editable();
         // Open dialog on double-clicks.
         editable.attachListener( editable, 'dblclick', function( evt ) {
            if ( editor.readOnly )
               return false;

            var data = { element: evt.data.getTarget() };
            editor.fire( 'doubleclick', data );
            data.dialog && editor.openDialog( data.dialog );

            return 1;
         });
      });
   }
});






